package cn.edu.lcu.cs.javaprogramming.clazz;

import cn.edu.lcu.cs.javaprogramming.BaseTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;
import java.util.StringJoiner;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 字符串演示
 *
 * @author ling
 * @date 2022/4/1 23:16
 */
public class StringDemo extends BaseTest {
    /**
     * 将带下划线的字符串转换为驼峰模式<br>
     * 如：map_under_score_to_camel_case to MapUnderScoreToCamelCase
     *
     * @param underScoreString 转换前的字符串
     */
    @ParameterizedTest
    @ValueSource(strings = {"map_under_score_to_camel_case"})
    void mapUnderScoreToCamelCase(String underScoreString) {
//        String camelCase = "";
        StringBuilder sb = new StringBuilder();
        // 代码填空
        String[] strings = underScoreString.split("_");
        System.out.println("underScoreString = " + underScoreString);
        System.out.println("Arrays.toString(strings) = " + Arrays.toString(strings));
        for (String string : strings) {
            // 将每个字符串首字母大写
            // 每次循环，都会在堆上分配一个空间给临时的新串
//            camelCase += toUpperCaseFirstLetter(string);
            sb.append(toUpperCaseFirstLetter(string));
        }


//        assertEquals("MapUnderScoreToCamelCase", camelCase);
        assertEquals("MapUnderScoreToCamelCase", sb.toString());
    }

    /**
     * 将字符串首字母大写
     *
     * @param src
     * @return
     */
    String toUpperCaseFirstLetter(String src) {
        // 代码填空
        char[] chars = src.toCharArray();
        // 转化第一个字符为大写
        chars[0] = Character.toUpperCase(chars[0]);

        return new String(chars);
    }

    /**
     * 将驼峰模式的字符串转换为下划线风格<br>
     * 如：MapUnderScoreToCamelCase to map_under_score_to_camel_case
     *
     * @param camelCase 转换前的字符串
     */
    @ParameterizedTest
    @ValueSource(strings = {"MapUnderScoreToCamelCase"})
    void mapCamelCaseToUnderScore(String camelCase) {
        String underScore = null;
        // 代码填空


        assertEquals("map_under_score_to_camel_case", underScore);
    }

    /**
     * 将字符串首字母小写
     *
     * @param src
     * @return
     */
    String toLowerCaseFirstLetter(String src) {
        // 代码填空


        return null;
    }

    /**
     * 字符串的相等判断，应该用equals方法。
     */
    @Test
    public void equals() {
        // 字符串是特殊的对象，在堆上分配，不可更改，唯一
        String lisi = "lisi";
        String lisi2 = "lisi";
        String lisi3 = "LisI".toLowerCase();

        System.out.println("lisi3 = " + lisi3);

        // 两两判断3个串是否相等
        // 代码填空
        // 两个引用都指向常量池中相同的地址，所以是相等的
        System.out.println("(lisi==lisi2) = " + (lisi == lisi2));
//        System.out.println("lisi==lisi2 = " + lisi == lisi2);
        System.out.println("(lisi==lisi3) = " + (lisi == lisi3));
        System.out.println("lisi.equals(lisi3) = " + lisi.equals(lisi3));

        System.out.println("\"Lisi\".equals(\"lisi\") = " + "Lisi".equals("lisi"));
        System.out.println("\"Lisi\".equalsIgnoreCase(\"lisi\") = " + "Lisi".equalsIgnoreCase("lisi"));


    }

    /**
     * 字符串拆分演示。
     */
    @Test
    public void split() {
        String string = "2021-04-18 22:23:28";
        // 依据正则表达式拆分字符串为字符串数组
        String[] split = null;
        // 代码填空
        split = string.split("[- :]");


        assertEquals("[2021, 04, 18, 22, 23, 28]", Arrays.toString(split));

        String url = "http://ee-c.lcu.edu.cn/";
        String[] split2 = null;
        // 代码填空
        split2 = url.split("[:/\\.]");
        assertEquals("[http, , , ee-c, lcu, edu, cn]", Arrays.toString(split2));
    }
    /**
     * 根据表名及列名数组，创建sql查询语句。
     */
    @Test
    void selectSql() {
        String table = "student";
        String[] columns = {"id", "name", "studentNo"};

        // 代码填空
        StringJoiner joiner = new StringJoiner(", ", "select ", " from " + table);
        for (String column : columns) {
            joiner.add(column);
        }

        assertEquals("select id, name, studentNo from student", joiner.toString());
    }

}
